import { getUserInfo } from '@/api/user'
import { Toast } from 'vant'
import { getItem } from '@/utils'

const CHANGE_USER_INFO = 'CHANGE_USER_INFO'
const state = {
  userInfo: null
}

const getters = {
  getUserInfo(state) {
    return state.userInfo
  }
}

const mutations = {
  [CHANGE_USER_INFO](state, obj) {
    state.userInfo = obj
  }
}

const actions = {
  changeUserInfo({commit}, userInfo) {
    commit(CHANGE_USER_INFO, userInfo)
  },
  getUserInfo({commit}) {
    const params = { token: getItem('token') }
    return getUserInfo(params).then(res => {
      if (res.resultCode === 200) {
        commit(CHANGE_USER_INFO, res.data)
        return res.data
      }
    }).catch(err => {
      Toast.fail('请求失败')
      console.log(err)
    })
  }
}

export default {
  namespaced: true,
  getters,
  state,
  mutations,
  actions
}